package org.dashboard.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdRole;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdRoleMapper;
import org.dashboard.service.AdRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = AdRoleService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER, cacheNames = "role")
public class AdRoleServiceImpl implements AdRoleService {
    private static final Logger logger = LoggerFactory.getLogger(AdRoleServiceImpl.class);

    @Autowired
    private AdRoleMapper adRoleMapper;


    @Override
    @CacheEvict(allEntries = true)
    public int deleteByPrimaryKey(Integer roleId) {
        return adRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int insert(AdRole record) {
        adRoleMapper.insert(record);
        return record.getRoleId();
    }

    @Override
    @CacheEvict(allEntries = true)
    public int insertSelective(AdRole record) {
        adRoleMapper.insertSelective(record);
        return record.getRoleId();
    }

    @Override
    @Cacheable(key = "'roleId:' + #roleId.toString()")
    public AdRole selectByPrimaryKey(Integer roleId) {
        return adRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKeySelective(AdRole record) {
        return adRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKey(AdRole record) {
        return adRoleMapper.updateByPrimaryKey(record);
    }


    @Override
    public List<AdRole> selectAdRoles() {
        return adRoleMapper.selectAdRoles();
    }


    @Override
    @Cacheable(key = "'roles_userId:' + #userId.toString()")
    public List<AdRole> selectUserAdRoles(Integer userId) {
        return adRoleMapper.selectUserAdRoles(userId);
    }


    @Override
    public List<Map<String, Object>> selectUserRoles(Integer userId) {
        return adRoleMapper.selectUserRoles(userId);
    }

    @Override
    public PageInfo selectAdRolesPaged(String criteria, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdRole> adRoles = adRoleMapper.selectAdRolesSearched(criteria);
        return new PageInfo<>(adRoles);
    }

    @Override
    @Transactional
    public boolean deleteByPrimaryKeys(ArrayList<Integer> roleIds) {
        for (Integer id : roleIds) {
            adRoleMapper.deleteByPrimaryKey(id);
        }
        return true;
    }


}
